package com.wyhello.tube4.spring.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.pojo.Position;
import com.wyhello.tube4.pojo.Reality;
import com.wyhello.tube4.pojo.Reside;
import com.wyhello.tube4.pojo.Tube;
import com.wyhello.tube4.spring.model.AnalysisDrawModel;
import com.wyhello.tube4.spring.model.MeterRecordsModel;
import com.wyhello.tube4.spring.model.ResideRecordsModel;
import com.wyhello.tube4.util.DateUtil;
@Controller
public class AnalysisDrawController {
	
	public static String[] POSITON_CODE = {"A","B","C","D","E","F","G"};
	/**
	 * 单管分析
	 */
	@Autowired
	private AnalysisDrawModel analysisDrawModel;
	@Autowired
	private MeterRecordsModel meterRecordsModel;
	@Autowired
	private ResideRecordsModel resideRecordsModel;
	/**
	 * 查询锅炉信息
	 * @return
	 */
	@RequestMapping("/analyse/queryRealitys")
	public ModelAndView queryRealitys(){
		List<Reside> boilers =  meterRecordsModel.queryResideByBoiler();
		ModelAndView model = new ModelAndView("main");
		model.addObject("boilers",boilers);
		return model;
	}
	/**
	 * 查询指定锅炉下的受热面
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/analyse/querySurface")
	public void querySurface(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Reside> surfaceSet = reside.getResides();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Reside surface : surfaceSet) {
			map.put(surface.getId(), surface.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	/**
	 * 查询指定受热面下的管排
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/analyse/queryRows")
	public void queryRows(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Reside> rows = reside.getResides();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Reside row : rows) {
			map.put(row.getId(), row.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	/**
	 * 查询指定管排下的管
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/analyse/queryTube")
	public void queryRow(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Tube> tubes = reside.getTubes();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Tube tube : tubes) {
			map.put(tube.getId(), tube.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	
	/**
	 * 查询指定管下的位置
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/analyse/queryPosition")
	public void queryPosition(int tubeid,HttpServletResponse response){
		Tube tube = resideRecordsModel.getTubeById(tubeid);
		Set<Position> positions = tube.getPositions();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Position position : positions) {
			map.put(position.getId(), position.getPositionName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	/**
	 *外径横向折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/diameterXTime")
	public ModelAndView diameterXTime(int tubeId,String begin,String end){
		
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end, "outerDiameterX");
		ModelAndView model = new ModelAndView("diameterX", "positions", positions);
		return model;
	}
	
	/**
	 *外径横向折现图 
	 * @param positionId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	/*@RequestMapping("/analyse/diameterXTime")
	public ModelAndView diameterXTime(int positionId,String begin,String end){
		
		List<Reality> realitys = analysisDrawModel.getMeasuringRecordByTimeAndPositionId(positionId,begin,end);
		ModelAndView model = new ModelAndView("diameterX", "realitys", realitys);
		return model;
	}*/
	
	/**
	 *外径纵向折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/diameterYTime")
	public ModelAndView diameterYTime(int tubeId,String begin,String end){
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end, "outerDiameterY");
		ModelAndView model = new ModelAndView("diameterY", "positions", positions);
		return model;
	}
	/**
	 *蠕脹横向折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/creepXTime")
	public ModelAndView creepXTime(int tubeId,String begin,String end){
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end, "creepValueX");
		ModelAndView model = new ModelAndView("creepX", "positions", positions);
		return model;
	}
	/**
	 *蠕脹纵向折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/creepYTime")
	public ModelAndView creepYTime(int tubeId,String begin,String end){
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end,"creepValueY");
		ModelAndView model = new ModelAndView("creepY", "positions", positions);
		return model;
	}
	/**
	 *实际壁厚折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/wallThickTime")
	public ModelAndView wallThickTime(int tubeId,String begin,String end){
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end,"wallThick");
		ModelAndView model = new ModelAndView("wallThick", "positions", positions);
		return model;
	}
	/**
	 *磨损速率折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/wearRateTime")
	public ModelAndView wearRateTime(int tubeId,String begin,String end){
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end,"wearRate");
		ModelAndView model = new ModelAndView("wearRate", "positions", positions);
		return model;
	}
	/**
	 *估算寿命折现图 
	 * @param tubeId 管ID
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return 
	 */
	@RequestMapping("/analyse/lifetimeTime")
	public ModelAndView lifetimeTime(int tubeId,String begin,String end){
		Set<Position> positions = analysisDrawModel.getMeasuringRecordByTime(tubeId,begin,end,"lifetime");
		ModelAndView model = new ModelAndView("lifetime", "positions", positions);
		return model;
	}

}
